//
//  LBTabBarController.m
//  app
//
//  Created by LINAICAI on 16/5/3.
//  Copyright © 2016年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBTabBarController.h"
#import "LBNavigationController.h"
#import <LBCategory/UIColor+LBColor.h>
@interface LBTabBarController ()

@end

@implementation LBTabBarController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    UIViewController *nv = self.selectedViewController;
    
    return [nv shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}
- (BOOL)shouldAutorotate

{
    UIViewController *nv = self.selectedViewController;
    return [nv shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{
    UIViewController *nv = self.selectedViewController;
    return [nv supportedInterfaceOrientations] ;//只支持这一个方向(正常的方向)
}
-(void)viewWillAppear:(BOOL)animated{
    [self.selectedViewController beginAppearanceTransition: YES animated: animated];
}
-(void) viewDidAppear:(BOOL)animated
{
    [self.selectedViewController endAppearanceTransition];
}
-(void) viewWillDisappear:(BOOL)animated
{
    [self.selectedViewController beginAppearanceTransition: NO animated: animated];
}
-(void) viewDidDisappear:(BOOL)animated
{
    [self.selectedViewController endAppearanceTransition];
}
- (instancetype)initWithControllers:(NSArray<UIViewController *> *)controllers
                         nomalImages:(NSArray<UIImage *> *)nomalImages
                        selectImages:(NSArray<UIImage *> *)selectImages
                        titles:(NSArray<NSString *> *)titles
                        selectIndex:(NSUInteger)selectIndex

{
    self = [super init];
    if (self) {
        NSMutableArray<UIViewController *> *vcArray = [NSMutableArray<UIViewController *> new];
        NSMutableArray<UITabBarItem *> *itemArray = [NSMutableArray<UITabBarItem *> new];
        
        for (NSInteger i = 0; i < controllers.count; i++) {
            LBNavigationController *vc = [[LBNavigationController alloc]initWithRootViewController:controllers[i]];
            UITabBarItem *item = [self itemWithImage:nomalImages[i] selectedImage:selectImages[i] title:titles[i] bindViewController:vc];
            
            [vcArray addObject: vc];
            [itemArray addObject:item];
        }
        [self setViewControllers:vcArray];
        [self setToolbarItems:itemArray animated:YES];
        [self setSelectedViewController:self.viewControllers[selectIndex]];

    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tabBar setTintColor:[UIColor blue]];

}
- (UIStatusBarStyle)preferredStatusBarStyle NS_AVAILABLE_IOS(7_0){
    return UIStatusBarStyleLightContent;
}
- (UITabBarItem *)itemWithImage:(UIImage *)image selectedImage:(UIImage *)selectImage title:(NSString *)title bindViewController:(UIViewController*)controller{
    UITabBarItem *item = [[UITabBarItem alloc]initWithTitle:title image:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[selectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                  [UIFont systemFontOfSize:12], NSFontAttributeName,nil]
                        forState:UIControlStateNormal];
    
    controller.tabBarItem = item;
    return item;
}

@end
